package com.ruoyi.web.controller.fire;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.fire.domain.TUnit;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.fire.domain.TDevice;
import com.ruoyi.fire.service.ITDeviceService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 设备信息Controller
 *
 * @author ruoyi
 * @date 2025-11-05
 */
@RestController
@RequestMapping("/system/device")
@Api(value = "TDeviceController", tags = "设备信息")
public class TDeviceController extends BaseController {
    @Autowired
    private ITDeviceService tDeviceService;

    /**
     * 设备信息列表
     */


    /**
     * 查询设备信息列表
     */
    @ApiOperation("查询设备信息列表")
    @PreAuthorize("@ss.hasPermi('system:device:list')")
    @GetMapping("/list")
    public TableDataInfo list(TDevice tDevice) {
        startPage();
        Long userId = SecurityUtils.getUserId();
        if (!SecurityUtils.isAdmin(userId)) {
            tDevice.setCreateUserId(userId);
        }
        List<TDevice> list = tDeviceService.selectTDeviceList(tDevice);
        return getDataTable(list);
    }

    /**
     * 导出设备信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:device:export')")
    @Log(title = "设备信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TDevice tDevice) {
        List<TDevice> list = tDeviceService.selectTDeviceList(tDevice);
        ExcelUtil<TDevice> util = new ExcelUtil<TDevice>(TDevice.class);
        util.exportExcel(response, list, "设备信息数据");
    }

    /**
     * 获取设备信息详细信息
     */
    @ApiOperation("获取设备信息详细信息")
    @PreAuthorize("@ss.hasPermi('system:device:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(tDeviceService.selectTDeviceById(id));
    }

    /**
     * 新增设备信息
     */
    @ApiOperation("新增设备信息")
    @PreAuthorize("@ss.hasPermi('system:device:add')")
    @Log(title = "设备信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TDevice tDevice) {
        return toAjax(tDeviceService.insertTDevice(tDevice));
    }

    /**
     * 修改设备信息
     */
    @ApiOperation("修改设备信息")
    @PreAuthorize("@ss.hasPermi('system:device:edit')")
    @Log(title = "设备信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TDevice tDevice) {
        return toAjax(tDeviceService.updateTDevice(tDevice));
    }

    /**
     * 删除设备信息
     */
    @ApiOperation("删除设备信息")
    @PreAuthorize("@ss.hasPermi('system:device:remove')")
    @Log(title = "设备信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(tDeviceService.deleteTDeviceByIds(ids));
    }


    /**
     * 手动同步设备信息
     */
    @ApiOperation("手动同步设备信息")
    @Log(title = "手动同步设备信息", businessType = BusinessType.OTHER)
    @GetMapping("/push/{id}")
    public AjaxResult push(@PathVariable String id) {
        return toAjax(tDeviceService.pushDevice(id));
    }

    /**
     * 远程删除设备信息
     */
    @ApiOperation("远程删除设备信息")
    @Log(title = "设备信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/remoteDeletion/{deviceIds}")
    public AjaxResult remoteDeletion(@PathVariable String deviceIds) {
        return toAjax(tDeviceService.remoteDeletion(deviceIds));
    }

    /**
     * 设备假数据同步
     */
    @ApiOperation("设备假数据同步")
    @PostMapping("/{deviceId}/{dataValue}")
    public AjaxResult fakeDataSynchronization(@PathVariable String deviceId,
                                              @PathVariable String dataValue) {
        tDeviceService.fakeDataSynchronization(deviceId, dataValue);
        return AjaxResult.success();
    }

    /**
     * 先删除远程在删除本地信息
     */
    @ApiOperation("先删除远程在删除本地信息")
    @Log(title = "楼层信息", businessType = BusinessType.OTHER)
    @GetMapping("/remove/{deviceId}")
    public AjaxResult removeByDeviceId(@PathVariable String deviceId) {
        return toAjax(tDeviceService.removeByDeviceId(deviceId));
    }
}
